package com.lixw.springai.controller;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: OpenAIController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/8/26 20:10
 **/
@RestController
public class OpenAIController {
    @Resource
    private ChatModel chatModel;

    private ChatClient chatClient;

    @PostConstruct
    public void init() {
        chatClient = ChatClient.builder(chatModel).build();
    }

    @GetMapping("/chat")
    public String chat(@RequestParam(name = "question",defaultValue = "你是谁") String question)
    {
        String result = chatClient.prompt(question).call().content();
        System.out.println("chat result:" + result);
        return result;
    }

}